﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Model;

namespace BO
{
    /// <summary>
    /// Summary description for VYC07BO
    /// </summary>
    public class VYC07BO : DBConnectionBO
    {
        public VYC07BO():base()
        {
            //
        }

        /// <summary>
        /// Lay danh sach cac du lieu trong VYC07
        /// </summary>
        /// <returns></returns>
        public List<ModelVYC07> GetAllList()
        {
            var list = new List<ModelVYC07>();
            const string query = "SELECT [Id],[Ten],[UniqueId],[NgayCapNhat],[TrangThai] FROM [VYC-07]";
            var sqlDataAdapter = new SqlDataAdapter(query, connection);
            var dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable); // fill data to Datatable
            CloseConnection(); // Close connection to database
            for (var i = 0; i < dataTable.Rows.Count; i++)
            {
                var model = new ModelVYC07();

                model.Id = (dataTable.Rows[i][0] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][0]);
                model.Ten = (dataTable.Rows[i][1] == DBNull.Value) ? string.Empty : dataTable.Rows[i][1].ToString();
                model.UniqueId = (dataTable.Rows[i][2] == DBNull.Value) ? string.Empty : dataTable.Rows[i][2].ToString();
                model.NgayCapNhat = (dataTable.Rows[i][3] == DBNull.Value) ? DateTime.Now : (DateTime)dataTable.Rows[i][3];
                model.TrangThai = (dataTable.Rows[i][4] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][4]);

                list.Add(model);
            }
            return list;
        }

        /// <summary>
        /// Add new a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddData(ModelVYC07 model)
        {
            var query = "INSERT INTO [VYC-07]([Ten],[UniqueId],[NgayCapNhat],[TrangThai]) VALUES(N'"
                + model.Ten + "', '" + model.UniqueId + "', GETDATE(), " + model.TrangThai + ")";
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Edit a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int EditData(ModelVYC07 model)
        {
            var query = "UPDATE [VYC-07] SET " +
                        "Ten = N'" + model.Ten + "'" +
                        ",UniqueId = '" + model.UniqueId + "'" +
                        ",NgayCapNhat = GETDATE(), TrangThai = " + model.TrangThai +
                        " WHERE Id = '" + model.Id + "'";
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Delete a model in data
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int DeleteData(ModelVYC07 model)
        {
            var query = "DELETE FROM [VYC-07] " +
                        " WHERE Id = " + model.Id;
            var sqlCommand = new SqlCommand(query, connection);
            try
            {
                var status = sqlCommand.ExecuteNonQuery();
                CloseConnection(); // close connection
                return status;
            }
            catch (Exception)
            {
                CloseConnection();
                return 0;
                throw;
            }
        }

        /// <summary>
        /// Get by id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<ModelVYC07> GetById(int id)
        {
            var list = new List<ModelVYC07>();
            var query = "SELECT [Id],[Ten],[UniqueId],[NgayCapNhat],[TrangThai] FROM [VYC-07] WHERE Id = " + id;
            var sqlDataAdapter = new SqlDataAdapter(query, connection);
            var dataTable = new DataTable();
            sqlDataAdapter.Fill(dataTable); // fill data to Datatable
            CloseConnection(); // Close connection to database
            for (var i = 0; i < dataTable.Rows.Count; i++)
            {
                var model = new ModelVYC07();

                model.Id = (dataTable.Rows[i][0] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][0]);
                model.Ten = (dataTable.Rows[i][1] == DBNull.Value) ? string.Empty : dataTable.Rows[i][1].ToString();
                model.UniqueId = (dataTable.Rows[i][2] == DBNull.Value) ? string.Empty : dataTable.Rows[i][2].ToString();
                model.NgayCapNhat = (dataTable.Rows[i][3] == DBNull.Value) ? DateTime.Now : (DateTime)dataTable.Rows[i][3];
                model.TrangThai = (dataTable.Rows[i][4] == DBNull.Value) ? 0 : Convert.ToInt32(dataTable.Rows[i][4]);

                list.Add(model);
            }
            return list;
        }
    }
}